package com.example.teaknower.fragment;



import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.teaknower.R;
import com.example.teaknower.activity.ArticledetailsActivity;
import com.example.teaknower.activity.MainActivity;
import com.example.teaknower.adapter.ArticleRecycleViewAdapter;
import com.example.teaknower.model.article;
import com.example.teaknower.utils.GlideImageLoader;
import com.example.teaknower.utils.OkHttpUtil;
import com.example.teaknower.utils.UserUtil;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class JingyingFragment extends Fragment  {
    @BindView(R.id.recycler1)
    RecyclerView mRecyclerView;
    Unbinder unbinder;
    private List<article> mdata = new ArrayList<>();
    private List<String> imageUrl = new ArrayList<>();
    int mPosition;
    private ArticleRecycleViewAdapter mAdapter;
    private Banner mBanner;
    private MainActivity MainActivity;
    public JingyingFragment(){
        getDate();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_jingying, container, false);
        unbinder = ButterKnife.bind(this, view);
        mPosition = 10;
        initView();
        initData();

        return view;
    }
    private void initView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new ArticleRecycleViewAdapter(R.layout.article_item_view, mdata);

//        View top = getLayoutInflater().inflate(R.layout.layout_banner, (ViewGroup) mRecyclerView.getParent(), false);
//        mBanner = top.findViewById(R.id.banner);
//        mAdapter.addHeaderView(top);
        mRecyclerView.setAdapter(mAdapter);

    }

    private void initData() {

        mAdapter.setNewData(mdata);//模拟网络请求成功后要调用这个方法刷新数据
        MainActivity = com.example.teaknower.activity.MainActivity.getMainActivity();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                //Toast.makeText(MainActivity,"点击文章的id为 ： " + mdata.get(position).getId(),Toast.LENGTH_LONG).show();
                Log.e("经营中点击文章的id",""+mdata.get(position).getId());
                Intent intent = new Intent(MainActivity, ArticledetailsActivity.class);
                intent.putExtra("id",mdata.get(position).getId());
                //如果是登录状态，添加浏览记录
                UserUtil userUtil = new UserUtil(MainActivity);
                String Uid = /*UserUtil.getUserId()*/"8";
                if(!Uid.equals("")){
                    addRecord(Uid,mdata.get(position).getId());
                }
                MainActivity.startActivity(intent);
            }
        });
    }

    private void addRecord(String uid, String article_id ){
        String s = null;
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Record/addRecord?id=" + article_id + "&userId=" + uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initBanner(List<String> imageUrl) {
        mBanner.setImages(imageUrl)
                .setImageLoader(new GlideImageLoader())
                .setDelayTime(3000)
                .start();
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });
    }

    private void getDate(){
        String s = null;
        try {
            s = OkHttpUtil.getRequest("http://192.168.3.25:8080/Article/findArticleByType?type=3");
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(s);
        List<Object> mList = (List<Object>)jsonObject.get("data");
        for(int i = 0 ; i < mList.size() ; i++ ){
            Map<String,Object> m = (Map<String,Object>) mList.get(i);
            String id = m.get("id").toString();

            String date = m.get("date").toString();
            String img_url = m.get("img_url").toString();
            String title = m.get("title").toString();
            article article = new article(id, title, img_url, date);
            mdata.add(article);
        }
    }



}